<?php

class Map
{
    public static function getTilesInfo($tiles) // array
    {
        $result = array();
        if($tiles && is_array($tiles))
        {
            $ids = array();
            foreach($tiles as $tile)
            {
                $x = $tile[0];
                $y = $tile[1];
                $id = Map::getIdByXY($x, $y);
                $ids[] = $id;
            }
            if($ids && count($ids))
            {
                $query = 'SELECT * FROM `tiles` WHERE `id` IN ('.implode(',',$ids).')';
                $result = _database::toarray($query);
            }
        }
        return $result;
    }
    public static function getXYbyId($coord)
    {
        $x = substr($coord, 0, 5);
        $y = substr($coord, 5, 5);
        return array($x,$y);
    }

    public static function getIdByXY($x,$y)
    {
        return (int)(str_pad($x, 5, '0',STR_PAD_LEFT).str_pad($y, 5, '0',STR_PAD_LEFT));
    }

    public static function initTile($id,$x,$y,$ground_type,$water_type)
    {
        $query = 'INSERT INTO `tiles` SET 
`id` = '.(int)$id.',
`x` = '.(int)$x.',
`y` = '.(int)$y.',
`ground_type`='.(int)$ground_type.',
`water_type`='.(int)$water_type.'
 ON DUPLICATE KEY UPDATE
`x` = '.(int)$x.',
`y` = '.(int)$y.',
`ground_type`='.(int)$ground_type.',
`water_type`='.(int)$water_type.'';
        _database::query($query);
        rpc::_add_response('sql', $query);
    }
}
?>

